package org.example.aop;

import org.example.dao.UserDao;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * Company 源辰信息
 *
 * @author navy
 * @date 2024/4/20
 * Email haijunzhou@hnit.edu.cn
 */
// 基于接口实现的动态代理
public class JdkProxy {

    public static Object proxy(Object target){
        Object proxyObject = Proxy.newProxyInstance(
                target.getClass().getClassLoader(),
                target.getClass().getInterfaces(),
                new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        System.out.println("=================== 前置增强");
                        Object res = method.invoke(target, args);
                        System.out.println("=================== 前置增强");
                        return res;
                    }
                }
        );
        return proxyObject;
    }

    public static void main(String[] args) {
        A b = new B();
        A proxy = (A) proxy(b);
        proxy.test();

//        UserDao userDao = new UserDao();
//        UserDao userDao1 = (UserDao) proxy(userDao);
//        userDao1.deleteById(100);

    }

}
